﻿Ext.namespace('Ext.ux');
Ext.ux.ChartPanel = function(config){
	Ext.apply(this,config);
	if (this.extraTitle){
		this.title = this.title +'-'+this.extraTitle;
	}
	Ext.ux.ChartPanel.superclass.constructor.call(this);
};
Ext.extend(Ext.ux.ChartPanel,Ext.Panel, {
	pinURL : undefined,
	pingURLParams : {},
	pingSuccess : false,
	autoRefresh : false,
	autoRefreshInterval : 0,
	flashvars : {},
	lazyRender : false,
	initComponent : function() {
		this.flashTemplate = new Ext.XTemplate(
				'<div style="width:100%;height:100%" ><ifarme width="100%" height="100%" frameborder=0 margin=0>',
				'<object id="{flashId}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="{swfWidth}" height="{swfHeight}">',
				'<param name="movie" value="{swf}?ChartNoDataText=没有数据&XMLLoadingText=加载数据&LoadDataErrorText=加载数据出错&InvalidXMLText=无效的数据" />',
				'<param name="quality" value="high" />',
				'<param name="wmode" value="transparent" />',
				'<param name="flashvars" value="{computedflashvars}" />',
				'<param name="allowScriptAccess" value="domain" />',
				'<param name="align" value="t" />',
				'<param name="salign" value="TL" />',
				'<param name="swliveconnect" value="true" />',
				'<param name="scale" value="showall" />',
				'<embed name="{flashId}" src="{swf}?ChartNoDataText=没有数据" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="{computedflashvars}" type="application/x-shockwave-flash" width="{swfWidth}" height="{swfHeight}" wmode="transparent" allowScriptAccess="always" swliveconnect="true" align="t" salign="TL" scale="noscale"></embed>',
				'</object>', '</ifame></div>');	
		this.flashTemplate.compile();

		Ext.ux.ChartPanel.superclass.initComponent.call(this);
	},

	afterRender : function() {
		Ext.ux.ChartPanel.superclass.afterRender.call(this);
		if (this.pingURL && !this.pingSuccess){
			this.task = {
			    run: function(){
					Ext.Ajax.request({
						url : this.pingURL,
						params : this.pingParams,
						success : function(response) {
							var resp = Ext.decode(response.responseText);
							if (resp){
								this.pingSuccess = true;
								Ext.TaskMgr.stop(this.task);
								
								this.renderFlash();
							}
						}.createDelegate(this)
					});
			    }.createDelegate(this),
			    interval: 10000 //10 second
			}
			Ext.TaskMgr.start(this.task);
			var html = '<table width=100% height=100%><tr><td align=center valign=center>没有数据</td></tr></table>';
			//if (this.body.first())
			//	Ext.getDom(this.body.first()).innerHTML=html;
			//else
			Ext.getDom(this.body).innerHTML=html;
		}else {
			if (!this.lazyRender){
				this.renderFlash();
			}
		}
	},

	calcDataURL : function() {
		if (this.dataURL) {
			if (!this.flashvars) {
				this.flashvars = {};
			}
			p = this.baseParams;
			if (typeof p == "object") {
				p = Ext.urlEncode(p);
			}
			url = this.dataURL;
			url += (url.indexOf('?') != -1 ? '&' : '?') + p;
			Ext.apply(this.flashvars, {
				dataURL : url
			});
		}
	},

	renderFlash : function() {
		this.flashId = Ext.id();
		this.calcDataURL();
		if (this.flashvars && (typeof this.flashvars == 'object')) {
			var tempflashvars = Ext.apply({}, this.flashvars);
			for (var key in tempflashvars) {
				if (typeof tempflashvars[key] == 'function') {
					tempflashvars[key] = tempflashvars[key].call(this, true);
				}
			};
			this.computedflashvars = Ext.urlEncode(tempflashvars);
		}
		//this.swfHeight = this.body.getSize().height - 2;
		//this.swfWidth = this.body.getSize().width - 2;
		this.swfWidth = '100%';
		this.swfHeight = '100%';
		
		if (this.body.first()){
			this.flashTemplate.overwrite(this.body, this);
		}
		else
			this.flashTemplate.insertFirst(this.body, this);
		if (this.autoRefresh && this.autoRefreshInterval>0){
			new Ext.util.DelayedTask(this.refresh,this).delay(this.autoRefreshInterval);
		}
	},

	getFlash : function() {
		var id = this.flashId;
		if (window.document[id]) {
			return window.document[id];
		}
		if (navigator.appName.indexOf("Microsoft Internet") == -1) {
			if (document.embeds && document.embeds[id])
				return document.embeds[id];
		} else {
			return document.getElementById(id);
		}
	},

	loadFlash : function(config) {
		Ext.apply(this, config);
		this.renderFlash();
	},
	
	refresh : function(){
		var flashObj = this.getFlash();
		if (flashObj){
			flashObj.style.display = 'none';
			for (var x in flashObj) {
				if (typeof flashObj[x] == 'function') {
					flashObj[x] = function(){};
				}
			}
		}
		this.renderFlash();
	}
});

Ext.ux.TimeChartPanel = function(config) {
	Ext.apply(this, config);

	this.createToolbar();

	Ext.apply(this.baseParams, this.getDateRange(this.dateMenu[0]));

	Ext.ux.TimeChartPanel.superclass.constructor.call(this, {
		tbar : this.toolBar
	});
};

Ext.extend(Ext.ux.TimeChartPanel, Ext.ux.ChartPanel, {
	dateMenu : [],
	extraMenu : [],
	createToolbar : function() {
		mi = [];
		for (i = 0; i < this.dateMenu.length; i++) {
			mi[mi.length] = this.getMenuItem(this.dateMenu[i]);
		}
		this.textItem = new Ext.Toolbar.TextItem(mi[0].text);
		items = [this.textItem, {
			xtype : 'tbfill'
		}];
		if (this.extraMenu && this.extraMenu.length > 0) {
			items = items.concat(this.extraMenu);
		}
		items = items.concat([{
			text : '时间设置',
			menu : {
				items : mi
			}
		}]);
		this.toolBar = new Ext.Toolbar({
			items : items
		});
	},

	getMenuItem : function(value) {
		if (value === '12') {
			return {
				text : '12小时',
				handler : this.onRefresh.createDelegate(this, ['12'])
			};
		} else if (value === '24') {
			return {
				text : '24小时',
				handler : this.onRefresh.createDelegate(this, ['24'])
			};
		} else if (value === 'd0') {
			return {
				text : '今天',
				handler : this.onRefresh.createDelegate(this, ['d0'])
			};
		} else if (value === 'd1') {
			return {
				text : '昨天',
				handler : this.onRefresh.createDelegate(this, ['d1'])
			};
		} else if (value === 'd7') {
			return {
				text : '7天',
				handler : this.onRefresh.createDelegate(this, ['d7'])
			};
		} else if (value === 'd30') {
			return {
				text : '30天',
				handler : this.onRefresh.createDelegate(this, ['d30'])
			};
		}
	},

	getDateRange : function(value) {
		var dt = new Date();
		var from = null;
		var to = null;
		if (value == '12') { // 12小时
			var dt1 = dt.add(Date.HOUR, -12);
			from = dt1.format('Y-m-d H:i:s');
			to = dt.format('Y-m-d H:i:s');
		} else if (value == '24') { // 24小时
			var dt1 = dt.add(Date.HOUR, -24);
			from = dt1.format('Y-m-d H:i:s');
			to = dt.format('Y-m-d H:i:s');
		} else if (value == 'd0') {// 今天
			var dt1 = dt.add(Date.HOUR, -dt.getHours()).add(Date.MINUTE, -dt.getMinutes()).add(Date.SECOND,
					-dt.getSeconds());
			from = dt1.format('Y-m-d H:i:s');
			to = dt.format('Y-m-d H:i:s');
		} else if (value == 'd1') {// 昨天
			var dt1 = dt.add(Date.DAY, -1).add(Date.HOUR, -dt.getHours()).add(Date.MINUTE, -dt.getMinutes()).add(
					Date.SECOND, -dt.getSeconds());
			from = dt1.format('Y-m-d H:i:s');
			to = dt1.add(Date.DAY, 1).format('Y-m-d H:i:s');

		} else if (value == 'd7') {// 过去7天
			var dt1 = dt.add(Date.DAY, -7);
			//.add(Date.HOUR, -dt.getHours()).add(Date.MINUTE, -dt.getMinutes()).add(
			//		Date.SECOND, -dt.getSeconds());
			from = dt1.format('Y-m-d H:i:s');
			to = dt.format('Y-m-d H:i:s');

		} else if (value == 'd30') {// 过去30天
			var dt1 = dt.add(Date.DAY, -30);
			//.add(Date.HOUR, -dt.getHours()).add(Date.MINUTE, -dt.getMinutes()).add(
			//		Date.SECOND, -dt.getSeconds());
			from = dt1.format('Y-m-d H:i:s');
			to = dt.format('Y-m-d H:i:s');

		}
		return {
			from : from,
			to : to
		}
	},

	onRefresh : function(range) {
		if (!this.baseParams){
			this.baseParams = {};
		}
		Ext.apply(this.baseParams, this.getDateRange(range));
		Ext.getDom(this.textItem.getEl().id).innerHTML = this.getMenuItem(range).text;
		this.renderFlash();
	}
});
